package com.he.cmsservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.he.baseservice.exceptionhandler.EduException;
import com.he.cmsservice.entity.CrmBanner;
import com.he.cmsservice.mapper.CrmBannerMapper;
import com.he.cmsservice.service.CrmBannerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 首页banner表 服务实现类
 * </p>
 *
 * @author testhejinyu
 * @since 2021-06-10
 */
@Service
public class CrmBannerServiceImpl extends ServiceImpl<CrmBannerMapper, CrmBanner> implements CrmBannerService {

    @Override
    public void addBanner(CrmBanner crmBanner) {
        int insert = baseMapper.insert(crmBanner);
        if (insert == 0){
            throw new EduException(2001,"添加轮播图失败");
        }
    }
    @Override
    public void deleteBanner(String id) {
        int delete = baseMapper.deleteById(id);
        if (delete == 0) {
            throw new EduException(20001,"删除轮播图失败");
        }
    }

    @Override
    public CrmBanner getBannerById(String id) {
        CrmBanner crmBanner = baseMapper.selectById(id);
        if (StringUtils.isEmpty(crmBanner)) {
            throw new EduException(20001,"根据id查询轮播图失败！");
        }
        return crmBanner;
    }

    @Override
    public void updateBanner(CrmBanner crmBanner) {
        int update = baseMapper.updateById(crmBanner);
        if (update == 0) {
            throw new EduException(20001,"更新轮播图信息事变");
        }
    }

    //开启redis 缓存
    @Cacheable(value = "banner",key = "'selectBannerList'")
    @Override
    public List<CrmBanner> findAllBanner() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.orderByDesc("sort");
        queryWrapper.last("limit 2");
        List<CrmBanner> list = baseMapper.selectList(queryWrapper);
        return list;
    }
}
